<template>
  <div>
    <div class="login-container">
      <el-form class="login-form">
        <div class="login-title">社区团购管理系统</div>
        <div v-if="loginType==1" class="form-item">
          <div class="form-label">用户名</div>
          <el-input class="form-input" placeholder="请输入用户名" v-model="rulesForm.username" prefix-icon="el-icon-user"></el-input>
        </div>
        <div v-if="loginType==1" class="form-item">
          <div class="form-label">密码</div>
          <el-input class="form-input" placeholder="请输入密码" v-model="rulesForm.password" type="password" prefix-icon="el-icon-lock"></el-input>
        </div>
        <div class="role-select" v-if="roles.length>1" prop="loginInRole">
          <el-radio-group v-model="rulesForm.role">
            <el-radio v-for="item in roles" :key="item.roleName" :label="item.roleName">{{item.roleName}}</el-radio>
          </el-radio-group>
        </div>
        <div class="btn-group">
          <el-button v-if="loginType==1" type="primary" class="login-btn" @click="login()">登 录</el-button>
          <el-button type="warning" class="register-btn" @click="register('shangjia')">注册商家</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>

import menu from "@/utils/menu";
export default {
  data() {
    return {
      baseUrl:this.$base.url,
      loginType: 1,
      rulesForm: {
        username: "",
        password: "",
        role: "",
        code: '',
      },
      menus: [],
      roles: [],
      tableName: "",
      codes: [{
        num: 1,
        color: '#000',
        rotate: '10deg',
        size: '16px'
      },{
        num: 2,
        color: '#000',
        rotate: '10deg',
        size: '16px'
      },{
        num: 3,
        color: '#000',
        rotate: '10deg',
        size: '16px'
      },{
        num: 4,
        color: '#000',
        rotate: '10deg',
        size: '16px'
      }],
    };
  },
  mounted() {
    let menus = menu.list();
    this.menus = menus;

    for (let i = 0; i < this.menus.length; i++) {
      if (this.menus[i].hasBackLogin=='是') {
        this.roles.push(this.menus[i])
      }
    }

  },
  created() {
    this.getRandCode()
  },
  destroyed() {
	    },
  components: {
  },
  methods: {

    //注册
    register(tableName){
		this.$storage.set("loginTable", tableName);
        this.$storage.set("pageFlag", "register");
		this.$router.push({path:'/register'})
    },
    // 登陆
    login() {

		if (!this.rulesForm.username) {
			this.$message.error("请输入用户名");
			return;
		}
		if (!this.rulesForm.password) {
			this.$message.error("请输入密码");
			return;
		}
		if(this.roles.length>1) {
			if (!this.rulesForm.role) {
				this.$message.error("请选择角色");
				return;
			}

			let menus = this.menus;
			for (let i = 0; i < menus.length; i++) {
				if (menus[i].roleName == this.rulesForm.role) {
					this.tableName = menus[i].tableName;
				}
			}
		} else {
			this.tableName = this.roles[0].tableName;
			this.rulesForm.role = this.roles[0].roleName;
		}

		this.$http({
			url: `${this.tableName}/login?username=${this.rulesForm.username}&password=${this.rulesForm.password}`,
			method: "post"
		}).then(({ data }) => {
			if (data && data.code === 0) {
				this.$storage.set("Token", data.token);
				this.$storage.set("role", this.rulesForm.role);
				this.$storage.set("sessionTable", this.tableName);
				this.$storage.set("adminName", this.rulesForm.username);
				this.$router.replace({ path: "/index/" });
			} else {
				this.$message.error(data.msg);
			}
		});
    },
    getRandCode(len = 4){
		this.randomString(len)
    },
    randomString(len = 4) {
      let chars = [
          "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
          "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
          "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
          "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
          "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
          "3", "4", "5", "6", "7", "8", "9"
      ]
      let colors = ["0", "1", "2","3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]
      let sizes = ['14', '15', '16', '17', '18']

      let output = [];
      for (let i = 0; i < len; i++) {
        // 随机验证码
        let key = Math.floor(Math.random()*chars.length)
        this.codes[i].num = chars[key]
        // 随机验证码颜色
        let code = '#'
        for (let j = 0; j < 6; j++) {
          let key = Math.floor(Math.random()*colors.length)
          code += colors[key]
        }
        this.codes[i].color = code
        // 随机验证码方向
        let rotate = Math.floor(Math.random()*60)
        let plus = Math.floor(Math.random()*2)
        if(plus == 1) rotate = '-'+rotate
        this.codes[i].rotate = 'rotate('+rotate+'deg)'
        // 随机验证码字体大小
        let size = Math.floor(Math.random()*sizes.length)
        this.codes[i].size = sizes[size]+'px'
      }
    },
  }
};
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);
  position: fixed;
  top: 0;
  left: 0;

  .login-form {
    width: 400px;
    height: 400px;
    padding: 30px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.15);
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .login-title {
    text-align: center;
    font-size: 22px;
    font-weight: bold;
    color: #333;
    margin-bottom: 30px;
  }

  .form-item {
    margin-bottom: 20px;
  }

  .form-label {
    font-size: 14px;
    color: #606266;
    margin-bottom: 8px;
  }

  .form-input {
    width: 100%;
    
    /deep/ .el-input__inner {
      height: 40px;
      line-height: 40px;
      border-radius: 4px;
      padding-left: 35px;
      border: 1px solid #DCDFE6;
      
      &:focus {
        border-color: #409EFF;
        box-shadow: 0 0 5px rgba(64,158,255,0.2);
      }
    }
    
    /deep/ .el-input__prefix {
      left: 10px;
    }
  }

  .role-select {
    margin: 20px 0;
    text-align: center;

    /deep/ .el-radio {
      margin: 0 8px;
      
      .el-radio__input {
        .el-radio__inner {
          border-color: #DCDFE6;
        }
      }

      .el-radio__label {
        color: #606266;
      }
    }
  }

  .btn-group {
    margin-top: 30px;
    text-align: center;
    
    .login-btn, .register-btn {
      width: 45%;
      height: 40px;
      border-radius: 4px;
      font-size: 14px;
      letter-spacing: 1px;
      
      &:hover {
        opacity: 0.9;
      }
    }
    
    .login-btn {
      margin-right: 8px;
    }
  }
}
</style>
